from datetime import datetime

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(20), nullable=False)
    password = db.Column(db.String(256), nullable=False)
    email = db.Column(db.String(50), nullable=False, unique=True)
    bio = db.Column(db.Text, default="这个人什么都没留下")
    join_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) # utcnow表示当前数据的加入时间
    status = db.Column(db.Boolean, default=True)
    is_admin = db.Column(db.Boolean, default=False)

class Message(db.Model):
    __tablename__ = 'message'  # 数据库表名

    id = db.Column(db.Integer, primary_key=True)  # 主键
    content = db.Column(db.Text, nullable=False)  # 用户消息内容
    bot_response = db.Column(db.Text, nullable=True)  # 机器人的消息内容
    timestamp = db.Column(db.DateTime, server_default=db.func.now())  # 时间戳
    # 外键
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

class Feedback(db.Model):
    __tablename__ = 'feedback'

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(500), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    user = db.relationship('User', backref=db.backref('feedbacks', lazy=True))
